﻿<!DOCTYPE html>
<!--培训记录管理b编辑-->
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="/static/js/common.js"></script>

    <link href="../static/js/vue-treeselect/vue-treeselect.css" rel="stylesheet" />
    <script src="../static/js/vue-treeselect/vue-treeselect.js"></script>
    <style>
        body {
            padding-bottom: 10px;
        }

        .vue-treeselect_style .vue-treeselect__control {
            height: 100%;
            border: none;
            background: none;
        }

        .vue-treeselect__placeholder {
            top: -3px;
        }

        .vue-treeselect__single-value {
            top: -3px;
        }
    </style>
</head>

<body>
    <div id="app" v-cloak class="popupW100">
        <el-form label-position="right" :model="Form" status-icon :rules="rules" ref="ruleForm" label-width="100px"
            size="mini">
            <el-row>
                <el-col :span="11">
                    <div class="elst-select-mini">
                        <el-form-item>
                            <el-input size="mini">
                                <template slot="prepend">
                                    <div> {{TrainingRecord.TrainingCoursewareName+mark.m}}</div>
                                </template>
                                <el-select @visible-change="visibleChange" v-model="Form.cbsCourseId" slot="suffix"
                                    :placeholder="TrainingRecord.TrainingCoursewareName">
                                    <el-option label="请选择" value=""></el-option>
                                    <el-option v-for="item in TrainingCoursewareName" :label="item.trainingCoursewareName"
                                        :value="item.id">
                                    </el-option>
                                </el-select>
                            </el-input>
                        </el-form-item>
                    </div>
                </el-col>
                <el-col :span="11" :offset="2">
                    <div class="elst-select-mini">
                        <el-form-item>
                            <el-input size="mini">
                                <template slot="prepend">
                                    <div> {{TrainingRecord.Type+mark.m}}</div>
                                </template>
                                <el-select @visible-change="visibleChange" v-model="Form.mark01" slot="suffix" :placeholder="TrainingRecord.Type">
                                    <el-option label="请选择" value=""></el-option>
                                    <el-option v-for="item in Type" :label="item.value" :value="item.key">
                                    </el-option>
                                </el-select>
                            </el-input>
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="11">
                    <div class="elst-select-mini">
                        <el-form-item>
                            <el-input size="mini">
                                <template slot="prepend">
                                    {{TrainingRecord.Trainer+mark.m}}
                                </template>
                                <tree-department-staff-single slot="suffix" v-model="Form.trainer" style="line-height:28px;"></tree-department-staff-single>

                            </el-input>
                        </el-form-item>
                    </div>
                </el-col>

                <el-col :span="11" :offset="2">
                    <el-form-item>
                        <div class="elst-select-mini" >
                            <el-input size="mini">
                                <template slot="prepend">
                                    {{TrainingRecord.TrainingDate+mark.m}}
                                </template>
                                <el-date-picker slot="suffix" v-model="Form.trainingDate" type="date" size="mini"
                                    format="yyyy-MM-dd" value-format="yyyy-MM-dd" placeholder="选择时间">
                                </el-date-picker>
                            </el-input>
                        </div>
                    </el-form-item>
                </el-col>
                <el-col :span="11">
                    <div class="elst-select-mini">
                        <el-form-item>
                            <el-input size="mini">
                                <template slot="prepend">
                                    <div> {{TrainingRecord.contractor+mark.m}}</div>
                                </template>
                                <el-select @visible-change="visibleChange" v-model="Form.cbsListID" slot="suffix" @change="postChange2"
                                    :placeholder="TrainingRecord.contractor">
                                    <el-option label="请选择" value=""></el-option>
                                    <el-option v-for="item in CBS_Lists" :label="item.contractorName"
                                               :value="item.id"></el-option>
                                </el-select>
                            </el-input>
                        </el-form-item>
                    </div>
                </el-col>
                <el-col :span="11" :offset="2">
                    <!--参与人-->
                    <div class="elst-select-mini vue-treeselect_style">
                        <el-form-item prop="OverdueScope">
                            <el-input size="mini">
                                <template slot="prepend">
                                    {{TrainingRecord.participant+mark.m}}
                                </template>
                                <!--<treeselect v-model="Form.participant" :multiple="true" :options="participant"
    :flat="true" :show-count="true" :disable-branch-nodes="true" slot="suffix"
    style="line-height:14px;" placeholder="请选择" />-->
                                <el-select @visible-change="visibleChange" v-model="Form.participant" multiple collapse-tags slot="suffix"
                                           placeholder="请选择">
                                    <el-option label="请选择" value=""></el-option>
                                    <el-option v-for="item in participant" :label="item.personnelName"
                                               :value="item.id" :key="item.id"></el-option>
                                </el-select>

                            </el-input>
                        </el-form-item>
                    </div>
                </el-col>
                <el-col :span="24">
                    <div class="elst-select-mini">
                        <el-form-item>
                            <div class="el-input--mini el-input-group">
                                <div class="el-input-group__prepend">
                                    {{TrainingRecord.Remarks+mark.m}}
                                </div>
                                <el-input class="el-textarea__inner" type="textarea" v-model="Form.remarks"
                                    autocomplete="off"></el-input>
                            </div>
                        </el-form-item>
                    </div>
                </el-col>
            </el-row>


            <el-row>
                <!--附件-->
                <el-col :xs="20" :sm="6" :md="6" :lg="12" :xl="4">
                    <div class="elst-select-mini">
                        <el-form-item>
                            <el-input type="text" v-model="Form.attachmentName" autocomplete="off" size="mini" disabled="true">
                                <template slot="prepend">
                                    {{TrainingRecord.Annex+mark.m}}
                                </template>
                            </el-input>
                        </el-form-item>
                    </div>
                </el-col>
                <!--附件上传按钮-->
                <el-col :xs="4" :sm="6" :md="6" :lg="12" :xl="4">
                    <el-upload name="files" class="fl" ref="Attachmentupload" action="/api/File/UploadFile"
                               :before-upload="beforeUpload" :on-success="successUpLoad" :auto-upload="true"
                               :show-file-list="false">
                        <!--:on-preview="handlePreview" :on-remove="handleRemove" :before-upload="fileAttachmentUploadbeforeupload"@click="btnAttachmentUpload"-->
                        <el-button type="primary">导入</el-button>
                    </el-upload>
                </el-col>
            </el-row>
            <el-row type="flex" justify="end">
                <el-form-item>
                    <el-button type="primary" @click="onSubmitForm('ruleForm','0')">{{col.btnSave}}</el-button>
                    <el-button type="primary" @click="onSubmitForm('ruleForm','1')">{{col.btnCommit}}</el-button>
                    <el-button type="primary" @click="onBack">{{col.btnBack}}</el-button>
                </el-form-item>
            </el-row>
        </el-form>

    </div>
    <script type="text/javascript">
        Vue.component('treeselect', VueTreeselect.Treeselect)

        var uploadText = "请选择上传文件";
        var app = new Vue({
            el: '#app',
            data: {
                id: '',
                //mydata: PersonInChargeTree,
                //业务表单内容
                Form: {
                    id: guidEmpty,
                    TrainingCoursewareName: '',
                    contractor: '',
                    Type: '',
                    Trainer: '',
                    TrainingDate: '',
                    participant: null,
                    cbS_TrainRecordUser:[],
                    editor: '',
                    editorTime: '',
                    Remarks: '',
                    attachmentId: '',
                    attachmentName: uploadText,
                },
                TrainingCoursewareName: [],
                participant: [],//参与人
                CBS_Lists: [],
                Type: [{ key: '入场培训', value: '入场培训' }, { key: '复训', value: '复训' }, { key: '专业知识', value: '专业知识' }],
                sex: [],
                delIds:[],
                //表单验证
                rules: {
                    Trainer: [
                        verification.NotNull,
                        { pattern: verification.StringLengthAtoB.t.format(2, 5), message: verification.StringLengthAtoB.m.format(2, 5), trigger: 'blur' }
                    ],

                    TrainingCoursewareName: [
                        verification.NotNull
                    ],
                    contractor: [
                        verification.NotNull
                    ],
                    Type: [
                        verification.NotNull
                    ],
                }
            },
            methods: {
                //解決edge下select bug
                visibleChange: function (isVisible) {
                    visibleChange(isVisible)
                },
                submitFileListUpload: function () {
                    //若更换文件则将文件名字更换触发条件--先删除上一个文件,后添加一个文件
                    if (this.labelUpload != uploadText) {
                        this.$refs.Fileupload.clearFiles();
                        this.labelUpload = file.name;
                    }
                    else {

                    }
                    //this.$refs.upload.submit();
                },
                handleRemove: function (file) {
                    console.log(file);
                },
                handlePreview: function (file) {
                    console.log(file.name);
                    window.open(file.url)
                },
                //上传前文件的校验
                beforeupload: function (file) {
                    var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
                    //将文件名称赋值到label上
                    this.labelUpload = file.name;
                    const extension = testmsg === 'xls'
                    const extension2 = testmsg === 'xlsx'
                    const isLt2M = file.size / 1024 / 1024 < 10

                    if (!extension && !extension2) {
                        this.$message({
                            message: '上传文件只能是 xls、xlsx格式!',
                            type: 'warning'
                        });
                    }
                    if (!isLt2M) {
                        this.$message({
                            message: '上传文件大小不能超过 10MB!',
                            type: 'warning'
                        });
                    }
                    if (extension || extension2 && isLt2M)
                        submitUpload(this, 'upload')
                    return extension || extension2 && isLt2M
                },
                //上传附件文件的校验
                fileAttachmentUploadbeforeupload: function (file) {
                    var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
                    //将附件名赋值到label上
                    this.Form.Annex = file.name;
                    const extension = testmsg === 'rar'
                    const extension2 = testmsg === 'zip'
                    const isLt2M = file.size / 1024 / 1024 < 10
                    if (!extension && !extension2) {
                        this.$message({
                            message: '上传文件只能是 rar、zip格式!',
                            type: 'warning'
                        });
                    }
                    if (!isLt2M) {
                        this.$message({
                            message: '上传文件大小不能超过 10MB!',
                            type: 'warning'
                        });
                    }
                    if (extension || extension2 && isLt2M)
                        submitUpload(this, 'upload')
                    return extension || extension2 && isLt2M
                },
                //提交数据
                onSubmitForm: function (formName, state) {
                    var mythis = this;
                    this.$refs[formName].validate(function (valid) {
                        //验证通过
                        if (valid) {
                            if (mythis.Form.cbsCourseId == null || mythis.Form.cbsCourseId == undefined || mythis.Form.cbsCourseId == "") {
                                alert('请选择培训课件名称');
                                return;
                            }
                            if (mythis.Form.mark01 == null || mythis.Form.mark01 == undefined || mythis.Form.mark01 == "") {
                                alert('请选择培训类型');
                                return;
                            }
                            if (mythis.Form.trainer == null || mythis.Form.trainer == undefined || mythis.Form.trainer == "") {
                                alert('请选择培训人');
                                return;
                            }
                            if (mythis.Form.trainingDate == null || mythis.Form.trainingDate == undefined || mythis.Form.trainingDate == "") {
                                alert('请选择培训日期');
                                return;
                            }
                            if (mythis.Form.cbsListID == null || mythis.Form.cbsListID == undefined || mythis.Form.cbsListID == "") {
                                alert('请选择承包商');
                                return;
                            }
                            if (mythis.Form.participant == null || mythis.Form.participant == undefined || mythis.Form.participant.length < 1) {
                                alert('请选择参与人');
                                return;
                            } 
                            mythis.Form.status = state;
                            var tempTRUser = mythis.Form.cbS_TrainRecordUser;
                            mythis.Form.cbS_TrainRecordUser = [];
                            for (var i = 0; i < mythis.Form.participant.length; i++) {
                               
                                ////var isbool = "0";
                                ////for (var j = 0; j < mythis.participant.length; j++) {
                                ////    if (mythis.participant[j].personnelName == mythis.Form.participant[i]) {
                                ////        for (var z = 0; z < mythis.Form.cbS_TrainRecordUser.length; z++) {
                                ////            if (mythis.Form.cbS_TrainRecordUser[z].userId == mythis.participant[j].id) {
                                ////                isbool = "1";
                                ////            }

                                ////        }
                                ////    }
                                ////}
                               
                                ////if (isbool == "0") {
                                //    for (var j = 0; j < mythis.participant.length; j++) {
                                //        if (mythis.Form.participant[i] == mythis.participant[j].personnelName) {
                                //            mythis.Form.cbS_TrainRecordUser.push({ userId: mythis.participant[j].id });
                                //        }
                                //    }
                                    
                                ////}
                                mythis.Form.cbS_TrainRecordUser.push({ userId: mythis.Form.participant[i] });
                            }


                            for (var i1 = 0; i1 < tempTRUser.length; i1++) {
                                var isExist = '0';
                                for (var j1 = 0; j1 < mythis.Form.cbS_TrainRecordUser.length; j1++) {
                                    if (tempTRUser[i1].userId == mythis.Form.cbS_TrainRecordUser[j1].userId) {
                                        isExist = "1";
                                    }
                                }
                                //如果不存在 代表删除
                                if (isExist == '0') {
                                    mythis.delIds.push(tempTRUser[i1].id);
                                }
                            }
                            for (var j1 = 0; j1 < mythis.Form.cbS_TrainRecordUser.length; j1++) {
                                var isExist = '0';
                                for (var i1 = 0; i1 < tempTRUser.length; i1++) {
                                    if (tempTRUser[i1].userId == mythis.Form.cbS_TrainRecordUser[j1].userId) {
                                        isExist = "1";
                                    }
                                }
                                //如果不存在
                                if (isExist == '0') {
                                    //mythis.delIds.push(tempTRUser[i1].id);
                                    tempTRUser.push(mythis.Form.cbS_TrainRecordUser[j1]);
                                }
                            }
                            mythis.Form.cbS_TrainRecordUser = tempTRUser;
                            //onSubmitForm(this, '/api/CBS_TrainRecord/Save', mythis.Form);
                            myAjax("post", mythis, '/api/CBS_TrainRecord/Save', mythis.Form, function (response1) {

                                if (mythis.delIds.length > 0) {
                                    myAjax("post", this, '/api/CBS_TrainRecordUser/DeleteMuti', mythis.delIds, function (response) {
                                        mythis.delIds = [];
                                        showMessage(msg.OperatingOk, 'success');
                                        parent.app.ListItem.dialogVisible = false;
                                    });
                                } else {
                                    showMessage(msg.OperatingOk, 'success');
                                    parent.app.ListItem.dialogVisible = false;
                                }

                            });
                        } else {
                            //验证失败
                            return false;
                        }
                    });
                },
                getData: function () {
                    //getData(this, '/api/CBS_TrainRecord/get?id=' + this.id);
                    var mythis = this;

                    if (mythis.id != null && mythis.id != '') {
                        myAjax("get", mythis, '/api/CBS_TrainRecord/get?id=' + mythis.id, null, function (response) {
                            var ajaxModel = response.data;

                            mythis.Form = ajaxModel;
                            //加载参与人 下拉
                            myAjax("post", mythis, '/api/CBS_User/GetList', { model: { cbsListID: mythis.Form.cbsListID, CreateFactory: null }, offset: 0, limit: 100 }, function (response) {
                                mythis.participant = response.data.pageData;
                                //console.log('h1', response.data.pageData);
                                //mythis.Form.participant = [];
                                //for (var i = 0; i < mythis.Form.cbS_TrainRecordUser.length; i++) {
                                //    mythis.Form.participant.push(mythis.Form.cbS_TrainRecordUser[i].id);
                                //}
                                var pTemp = [];
                                for (var i = 0; i < mythis.Form.cbS_TrainRecordUser.length; i++) {
                                    pTemp.push(mythis.Form.cbS_TrainRecordUser[i].userId);
                                    //for (var j = 0; j < mythis.participant.length; j++) {
                                    //    if (mythis.participant[j].id == mythis.Form.cbS_TrainRecordUser[i].userId) {
                                    //        pTemp.push(mythis.participant[j].personnelName);
                                    //    }
                                    //}
                                }
                                ////mythis.Form.participant = pTemp;
                                mythis.$set(mythis.Form, 'participant', pTemp);
                               
                            }, false);
                        }, false);
                          
                    }
                   
                },
                onBack: function () {
                    parent.app.ListItem.dialogVisible = false;
                },
                //上传附件文件的校验
                beforeUpload: function (file) {
                    const isJPG = file.type === 'image/jpeg';
                    const isPNG = file.type === 'image/png';
                    const isLt2M = file.size / 1024 / 1024 < 2;
                    //if (!isJPG && !isPNG) {
                    //    this.$message.error('仅可上传 JPG和PNG 格式!');
                    //}
                    if (!isLt2M) {
                        this.$message.error('上传图片大小不能超过 2MB!');
                    }
                    return isLt2M; //(isJPG || isPNG) && 
                },
                //上传成功后执行
                successUpLoad: function (res, file) {
                    var data = res.data;//成功后的业务数据
                    var fileModel = data[0];//第一个文件对象
                    //var d = new Date();
                    this.Form.attachmentId = fileModel.id;
                    this.Form.attachmentName = file.name;
                    //this.Form.cbs_list.cbS_ListAttachments.push({
                    //    attachment: fileModel.id,
                    //    path: downLoadPath(fileModel.id),
                    //    name: file.name,
                    //    cbS_ListId: this.id,
                    //    uploadTime: new Date(d.setHours(d.getHours() + 8)).toJSON().substr(0, 19).replace('T', ' '),
                    //    uploadUser: this.Form.sysUser.id,
                    //    sysUser: {
                    //        sysUserInfor: { name: this.Form.sysUser.name, }
                    //    }
                    //});
                },
                btnAttachmentUpload: function () {
                    //若更换文件则将文件名字更换触发条件--先删除上一个文件,后添加一个文件
                    if (this.fileAttachmentUpload != uploadText) {
                        this.$refs.Attachmentupload.clearFiles();
                        this.fileAttachmentUpload = file.name;
                    }
                    else {

                    }
                    this.$refs.Attachmentupload.submit();
                },
                //初始化课件
                getClassList: function () {
                    var mythis = this;
                    myAjax("post", this, '/api/CBS_Course/GetList', { model: { createFactory: null }, offset: 0, limit: 100 }, function (response) {
                        mythis.TrainingCoursewareName = response.data.pageData;

                        //console.log(response.data.pageData);
                    }, false);
                },
                //初始化承包商下拉框
                getCBS_List: function () {
                    var mythis = this;
                   
                    myAjax("post", mythis, '/api/CBS_List/GetList', { model: { status: '', CreateFactory: null }, offset: 0, limit: 100 }, function (response) {
                            mythis.CBS_Lists = response.data.pageData;
                            //console.log('h1', response.data.pageData);
                        }, false);
                    
                },
                postChange2: function (val) {
                    var mythis = this;
                    //加载参与人 下拉
                    myAjax("post", mythis, '/api/CBS_User/GetList', { model: { cbsListId: val, CreateFactory: null }, offset: 0, limit: 100 }, function (response) {
                        mythis.participant = response.data.pageData;
                        //console.log('h1', response.data.pageData);
                    }, false);

                }
            },
            mounted: function () {
                //this.participant = fn(this.mydata, "0");
                //this.TrainingCoursewareName = getDicData('54');
                //this.contractor = getDicData('139');
                //this.Type = getDicData('n101');
                //this.sex = getDicData('01');
                this.getClassList();
                this.getCBS_List();

                this.id = getUrlParam("id");
                this.getData();
            }
        });
    </script>
</body>

</html>